--
-- DESCRIPTION
--
-- @COMPANY **
-- @AUTHOR **
-- @DATE ${date} ${time}
--

require "UnLua"
local Screen = require "Screen"
local Debug = require "Debug"
local Const = require "Const"

local M = Class("Core/CBP_Base")

local Lerp = UE.UKismetMathLibrary.Lerp

function M:Initialize(Initializer)
end

function M:ReceiveBeginPlay()
  self.Super.ReceiveBeginPlay(self)

	self.DefaultFOV = self.Camera.FieldOfView
	self.TimerHandle = UE.UKismetSystemLibrary.K2_SetTimerDelegate({self, M.FallCheck}, 1.0, true)
	local InterpFloats = self.ZoomInOut.TheTimeline.InterpFloats
	local FloatTrack = InterpFloats:GetRef(1)
	FloatTrack.InterpFunc:Bind(self, M.OnZoomInOutUpdate)
end

function M:SpawnWeapon()
	local World = self:GetWorld()
	if not World then
		return
	end

	local WeaponClass = UE.UClass.Load(Const.DefaultWeapon_Class)
	-- local NewWeapon = World:SpawnActor(WeaponClass, self:GetTransform(), UE.ESpawnActorCollisionHandlingMethod.AlwaysSpawn, self, self, "Weapon.BP_DefaultWeapon_C")
	local sp = UE.FActorSpawnParameters()
	sp.SpawnCollisionHandlingOverride = UE.ESpawnActorCollisionHandlingMethod.AlwaysSpawn
	sp.Owner = self
	sp.Instigator = self

	-- local NewWeapon = World:SpawnActorEx(WeaponClass, self:GetTransform(), nil, "Core.Weapon.BP_DefaultWeapon", sp)
	return NewWeapon
end


function M:UserConstructionScript()
  self.JumpMaxCount = 3

  ----------------- Mesh -------------------
  self.Mesh:SetSkeletalMesh(LoadObject(Const.UE4_SKMesh))
  -- self.Mesh:SetSkeletalMesh(LoadObject(Const.SSP_SKMesh))
  -- self.Mesh:SetSkeletalMesh(LoadObject(Const.MW_Female))
  -- self:SetupManMasterPose()
	-- self.Mesh:SetSkeletalMesh(LoadObject(Const.MAP_Mesh))


  ----------------- AnimBP -------------------
  local AnimBP_Class = UE.UClass.Load(Const.UE4_AnimBP)
  -- local AnimBP_Class = UE.UClass.Load(Const.SSP_AnimBP)
  -- local AnimBP_Class = UE.UClass.Load(Const.AnimBP_Root)
  -- local AnimBP_Class = UE.UClass.Load(Const.MAP_AnimBP)
  
  self.Mesh:SetAnimClass(AnimBP_Class)


  ----------------- Transform-------------------
	self.Mesh:K2_SetRelativeLocation(UE.FVector(0.0, 0.0, -88), false, nil, false)
	self.Mesh:K2_SetRelativeRotation(UE.FRotator(0.0, -90, 0), false, nil, false)

  ----------------- Camera -------------------
	self.SpringArm:K2_SetRelativeLocation(UE.FVector(0, 70, 68), false, nil, false)
	self.Camera.FieldOfView = 80

  self.SpringArm.bUsePawnControlRotation = true
  self.bUseControllerRotationYaw = false
  self.CharacterMovement.bOrientRotationToMovement = true

  ----------------- Movement -------------------
  self.CharacterMovement.MaxWalkSpeed = 300
  self.bIsFly = false
end

function M:SetupManMasterPose()
	self.Mesh:SetSkeletalMesh(LoadObject(Const.SK_Man_Full))

  self.SubMeshList = {}
  self.SubMeshList[0] = {Mesh=self.Cloth_Up, Path=Const.SK_Man_Jacket}
  self.SubMeshList[1] = {Mesh=self.Pants, Path=Const.SK_Man_Pants}
  self.SubMeshList[2] = {Mesh=self.Shoes, Path=Const.SK_Man_Shoes}
  self.SubMeshList[3] = {Mesh=self.Bag, Path=Const.SK_Man_Bag}

  for i = 0, 3 do
  	local Sub = self.SubMeshList[i]
  	if Sub then
  		Sub.Mesh:SetSkeletalMesh(LoadObject(Sub.Path))
  		Sub.Mesh:SetMasterPoseComponent(self.Mesh, false)
  	end
  end
end

function M:GetWeaponTraceInfo()
	local TraceLocation = self.Camera:K2_GetComponentLocation()
	local TraceDirection = self.Camera:GetForwardVector()
	return TraceLocation, TraceDirection
end

function M:OnZoomInOutUpdate(Alpha)
	local FOV = Lerp(self.DefaultFOV, self.Weapon.AimingFOV, Alpha)
	self.Camera:SetFieldOfView(FOV)
end

function M:ReceiveDestroyed()
	UE.UKismetSystemLibrary.K2_ClearTimerHandle(self, self.TimerHandle)
end

function M:UpdateAiming(IsAiming)
  self.bIsAiming = IsAiming
	if self.Weapon then
		if IsAiming then
			self.ZoomInOut:Play()
		else
			self.ZoomInOut:Reverse()
		end
	end
end

function M:FallCheck()
	local Location = self:K2_GetActorLocation()
	if Location.Z < -200.0 then
    -- Screen.Print("CBP: < -200")
    -- UE.UKismetSystemLibrary.ExecuteConsoleCommand(self, "RestartLevel")
	end
end

function M:UpdateUI()
  local Controller = self:GetController()
  if Controller then
    -- Debug.Print(Controller)
    Controller:UpdateHp(self.AttrComp:GetHpPercent())
  end
end

function M:PlayMontage()
  -- will break animBP
  -- self.Mesh:PlayAnimation(LoadObject('/Game/Assets/tmp/Taunt.Taunt'), false)

  -- Debug.Print(self.PlayAnimMontage)

  local Montage = LoadObject('/Game/Assets/tmp/Taunt_Montage.Taunt_Montage')
  -- local ret = self.PlayAnimMontage(Montage, 1, UE.NAME_None)
  -- self.Mesh:PlayMontage(Montage)
  local ret = self.Mesh:GetAnimInstance():Montage_Play(Montage, 1,UE.EMontagePlayReturnType.MontageLength, 0, true)
  -- AnimInstance->Montage_JumpToSection(StartSectionName, AnimMontage);

  Screen.Print(string.format("T pressed %f", ret))	
end

----------------- Input -----------------
function M:T_Pressed()
	self:PlayMontage()
end


function M:F_Pressed()
  if self.bIsFlying then
    self.CharacterMovement:SetMovementMode(UE.EMovementMode.MOVE_Walking);
    self.bIsFlying = false
  else
    self.CharacterMovement:SetMovementMode(UE.EMovementMode.MOVE_Flying);
    self.bIsFlying = true
  end
end

function M:Aim_Pressed()
  -- self.bIsAiming = true
  -- self.SpringArm.TargetArmLength = 80
  self:UpdateAiming(true)
end

function M:Aim_Released()
  -- self.bIsAiming = false
  -- self.SpringArm.TargetArmLength = 300
  self:UpdateAiming(false)
end

function M:CrouchToggle_Pressed()
  if self:CanCrouch() then
    self:Crouch()
  else
    self:UnCrouch()
  end
end

function M:SprintToggle_Pressed()
  self.bIsSprinting = true

  local MaxSpeed = self.CharacterMovement.MaxWalkSpeed
  if MaxSpeed >= 900 then
  	self.bIsSprinting = false
  	MaxSpeed = 300
  else
  	MaxSpeed = MaxSpeed+200
  end

  Screen.Print(MaxSpeed)
  self.CharacterMovement.MaxWalkSpeed = MaxSpeed
end

function M:SprintToggle_Released()
  -- self.bIsSprinting = false
  -- self.CharacterMovement.MaxWalkSpeed = 300
end


return M
